#include "main.h"
#include <string.h>

#define RECV_BUF_SIZE 512
static uint8_t recv_buf[RECV_BUF_SIZE];
static uint16_t recv_buf_index = 0;
static uint16_t write_buf_index = 0;


//////////////////////cmd proc/////////////////////////
#define FRAME_HEAD 0xF5
#define FRAME_HEAD_1 0x5F
#define FRAME_CMD 0xAA

#define FRAME_STEP_HEAD 0x00
#define FRAME_STEP_HEAD_2 0x01
#define FRAME_STEP_LEN 0x02
#define FRAME_STEP_CMD 0x03

#define FRAME_CMD_SIZE 128


static uint8_t step = FRAME_STEP_HEAD;
static uint8_t frame_len = 0;
static uint8_t index_len = 0;
static uint8_t cmd[FRAME_CMD_SIZE];

void proc_cmd_handle(uint8_t dat[], uint8_t len);
void data_proc(uint8_t dat);
///////////////////////////////////////////////////////


void router_init(void)
{
    GPIO_Init(RS485_EN_PORT, (GPIO_Pin_TypeDef)RS485_EN_PIN, GPIO_MODE_OUT_PP_LOW_FAST);
    GPIO_WriteLow(RS485_EN_PORT, RS485_EN_PIN);
}


void sp485_enable(char send)
{
    if(send){
        //enable 485
        GPIO_WriteHigh(RS485_EN_PORT, RS485_EN_PIN);

    }
    else
    {
        //disable 485
        GPIO_WriteLow(RS485_EN_PORT, RS485_EN_PIN);
    }
}



void send_485_str(uint8_t *str, uint8_t len)
{
    uint8_t i;
    //en 485
    sp485_enable(1);
    for(i = 0; i < len; i++)
    {
        UART1_SendData8(str[i]);   
    }
    //disabled 485
    SysTimer_DelayMs(10);
    sp485_enable(0);
}



void send_router(uint8_t cmd)
{
    //en 485
    sp485_enable(1);

    UART1_SendData8(cmd);   
    //disabled 485
    SysTimer_DelayMs(10);
    sp485_enable(0);

}


void recv_485_handle(uint8_t dat)
{
    write_buf_index = ((++write_buf_index)%RECV_BUF_SIZE);
    recv_buf[write_buf_index] = dat;
}

void recv_485_process(void)
{
    if(recv_buf_index != write_buf_index)
    {
        recv_buf_index = ((++recv_buf_index)%RECV_BUF_SIZE);
        // send_router(recv_buf[recv_buf_index]);
        data_proc(recv_buf[recv_buf_index]);
    }
}

void data_proc(uint8_t dat)
{
    switch(step)
    {
        case FRAME_STEP_HEAD:
            if(dat == FRAME_HEAD)
            {
                step = FRAME_STEP_HEAD_2;
                frame_len = 0;
                index_len = 0;
            }
            else
            {
                step = FRAME_STEP_HEAD;
            }
            break;
        case FRAME_STEP_HEAD_2:
            if(dat == FRAME_HEAD_1)
            {
                step = FRAME_STEP_LEN;
            }
            else
            {
                step = FRAME_STEP_HEAD;
            }
            break;
        case FRAME_STEP_LEN:
            if(dat <= 128)
            {
                frame_len = dat;
                index_len = 0;
                memset(cmd, 0, sizeof(cmd));
                step = FRAME_STEP_CMD;
            }  
            else{
                step = FRAME_STEP_HEAD;
            }
            break;
        case FRAME_STEP_CMD:
            if(index_len < frame_len)
            {
                cmd[index_len++] = dat;
                if(index_len == frame_len)
                {
                    proc_cmd_handle(cmd, frame_len);
                    step = FRAME_STEP_HEAD;    
                }
            }
            else
            {
                proc_cmd_handle(cmd, frame_len);
                step = FRAME_STEP_HEAD;              
            }
            break;
        default:
            step = FRAME_STEP_HEAD;
            break;
    }
    
}

void proc_cmd_handle(uint8_t dat[], uint8_t len)
{
    // uint8_t i = 0;
    // for(i = 0; i < len; i++)
    // {
    //     send_router(dat[i]);
    // }
    cmd_lock_handle(dat, len);
}
